package csu.webcourse.mypetstore.web.servlet;

import csu.webcourse.mypetstore.domain.Account;
import csu.webcourse.mypetstore.domain.Order;
import csu.webcourse.mypetstore.persistence.OrderDao;
import csu.webcourse.mypetstore.service.LogService;
import csu.webcourse.mypetstore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class ViewOrder2Servlet extends HttpServlet {

    private static final String VIEWORDER = "/WEB-INF/jsp/order/viewOrder.jsp";

    private int orderId;
    private Order order;
    private OrderService orderService;
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        orderId = Integer.parseInt(req.getParameter("orderId"));
        orderService = new OrderService();
        order = orderService.getOrder(orderId);
        HttpSession session = req.getSession();
        session.setAttribute("order", order);

        Account loginAccount = (Account) session.getAttribute("loginAccount");
        if (loginAccount != null) {
            HttpServletRequest httpRequest = req;
            String strBackUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                    + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());

            LogService logService = new LogService();
            //最后加入的信息“XXXXX”应当为该界面的信息以及一些商品信息
            String logInfo = logService.logInfo(" ") + strBackUrl + "用户请求查看订单";
            logService.insertLogInfo(loginAccount.getUsername(), logInfo);
        }

        req.getRequestDispatcher(VIEWORDER).forward(req,resp);
    }
}
